//
//  FileUtils.m
//  imessagesender
//
//  Created by Pang lei on 13-7-25.
//  Copyright (c) 2013年 Pang lei. All rights reserved.
//

#import "FileUtils.h"
#import "ZipArchive.h"

@implementation FileUtils 

+ (void)createDirectory:(NSString *)filePath
{
    
    NSError *error;
    
    if (![[NSFileManager defaultManager] createDirectoryAtPath:filePath
                                   withIntermediateDirectories:NO
                                                    attributes:nil
                                                         error:&error]) {
        NSLog(@"Create directory error: %@", error);
    }
}

+ (void)unzip:(NSString *)zipFile workspace:(NSString *)file
{
    ZipArchive *zip = [[ZipArchive alloc] init];
    if( [zip UnzipOpenFile:zipFile] ) {
        BOOL ret = [zip UnzipFileTo:file overWrite:YES];
        if( NO==ret ) { }
        [zip UnzipCloseFile];
    }
}

+ (void)appendToFile:(NSString *)filepath content:(NSString *)content
{
    NSError *err;
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filepath];
    if(fileHandle) {
        NSString *contents = [NSString stringWithContentsOfFile:filepath encoding:NSUnicodeStringEncoding error:&err];
        contents = [contents stringByAppendingString:content];
        [contents writeToFile:filepath atomically:YES encoding: NSUnicodeStringEncoding error:&err];
    } else {
        [content writeToFile:filepath atomically:NO encoding: NSUnicodeStringEncoding error:&err];
    }
}

@end
